package com.ruoyi.web.controller.planlog;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BeCompleted;
import com.ruoyi.domain.PlanToLog;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IPlanToLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 计划日志Controller
 *
 * @author ruoyi
 * @date 2020-06-28
 */
@Controller
@RequestMapping("/ruoyi/log")
public class PlanToLogController extends BaseController {
    private String prefix = "ruoyi/log";

    @Autowired
    private IPlanToLogService planToLogService;

    @RequiresPermissions("ruoyi:log:view")
    @GetMapping()
    public String log() {
        return prefix + "/log";
    }

    /**
     * 详情页面传递参数
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:logs:view")
    @GetMapping("/logs/{bId}")
    public String logs(@PathVariable("bId") Long bId, ModelMap map) {
        PlanToLog planToLog = planToLogService.selectPlanToLogById(bId);
        map.put("planToLog", planToLog);
        return prefix + "/logs";
    }

    /**
     * 查询计划日志列表
     */
    @RequiresPermissions("ruoyi:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlanToLog planToLog) {
        startPage();
        List<PlanToLog> list = planToLogService.selectPlanToLogList(planToLog);
        return getDataTable(list);
    }

    /**
     * 查询总项目下对应的计划日志列表
     */
    @RequiresPermissions("ruoyi:log:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long sid) {
        startPage();
        List<PlanToLog> list = planToLogService.selectPlanToLogLists(sid);
        return getDataTable(list);
    }

    /**
     * 首页下对应的计划日志列表
     */
    @RequiresPermissions("ruoyi:log:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long idss) {
        startPage();
        List<PlanToLog> list = planToLogService.selectPlanToLogLists(idss);
        return getDataTable(list);
    }

    /**
     * 导出计划日志列表
     */
    @RequiresPermissions("ruoyi:log:export")
    @Log(title = "计划日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlanToLog planToLog) {
        List<PlanToLog> list = planToLogService.selectall(planToLog);
        ExcelUtil<PlanToLog> util = new ExcelUtil<PlanToLog>(PlanToLog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 新增计划日志
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存计划日志
     */
    @RequiresPermissions("ruoyi:log:add")
    @Log(title = "计划日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, PlanToLog planToLog) throws IOException {
        //上传文件路径
        String filePach = Global.getUploadPath();
        //上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePach, file);
        planToLog.setpData(fileName);
        String name = ShiroUtils.getLoginName();
        planToLog.setpTheHeir(name);
        planToLog.setpTime(new Date());
        return toAjax(planToLogService.insertPlanToLog(planToLog));
    }

    /**
     * 修改计划日志
     */
    @GetMapping("/edit/{pId}")
    public String edit(@PathVariable("pId") Long pId, ModelMap mmap) {
        PlanToLog planToLog = planToLogService.selectPlanToLogById(pId);
        mmap.put("planToLog", planToLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存计划日志
     */
    @RequiresPermissions("ruoyi:log:edit")
    @Log(title = "计划日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlanToLog planToLog) {
        return toAjax(planToLogService.updatePlanToLog(planToLog));
    }

    /**
     * 删除计划日志
     */
    @RequiresPermissions("ruoyi:log:remove")
    @Log(title = "计划日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(planToLogService.deletePlanToLogByIds(ids));
    }

    /**
     *
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
    /**
     * 提交申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long pId) {
        int leave = planToLogService.submitExaStatus(pId);
        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long pId) {
        int leave = planToLogService.revoExaStatus(pId);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }
}
